﻿using OpenTK.Mathematics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AssetImporter.VxlConvert
{
    public class Palette
    {
		static int Size = 256;

		public readonly string Name;
        public readonly Color4[] Colors;
		public int[] Remaps = new int[0];

		public Palette(Stream s, string name)
        {
            Name = name;
            Colors = new Color4[Palette.Size];
			for (var i = 0; i < Palette.Size; i++)
			{
				byte a = 255;
				var r = (byte)(s.ReadByte() << 2);
				var g = (byte)(s.ReadByte() << 2);
				var b = (byte)(s.ReadByte() << 2);

				// Replicate high bits into the (currently zero) low bits.
				r |= (byte)(r >> 6);
				g |= (byte)(g >> 6);
				b |= (byte)(b >> 6);

				Colors[i] = new Color4(r,g,b,a);
			}
		}

		public static Palette Load(string filename, in string name)
		{
			using (var s = File.OpenRead(filename))
				return new Palette(s, name);
		}
	}
}
